<template>
  <!--
1.页面 el-card
2.设置标题头部 header template
3.设置总表单 el-form 设置model属性 rules规则
4.设置表单 el-form-item 在表带内添加 el-input 绑定v-model 与 el-button绑定prop属性 、
-->
  <div class="login-page">
    <el-card>
      <template #header>黑马面经运营后台</template>
      <el-form :model="form" :rules="rules" ref="loginForm">
        <el-form-item prop="username" label="用户名：">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password" label="密码：">
          <el-input v-model="form.password" placeholder="请输入密码：" />
        </el-form-item>
        <el-form-item>
          <el-button :plain="true" @click="hlogin" type="primary"
            >登录</el-button
          >
          <el-button @click="hreset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
export default {
  name: 'login-page',
  data () {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', tigger: 'blur' },
          { min: 6, max: 15, message: '长度在6~15位之间', tigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', tigger: 'blur' },
          { min: 5, max: 10, message: '长度在5~10位之间', tigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    hlogin () {
      this.$refs.loginForm.validate(async (valid) => {
        console.log(valid)
        if (!valid) return
        // console.log('发送请求')
        const res = await login(this.form)
        // console.log(res)
        this.$store.commit('user/setUserToken', res.data.token)
        this.$router.push('/')
        this.$message.success({
          message: '恭喜你,登录成功',
          type: 'success'
        })
      })
    },
    hreset () {
      this.$refs.loginForm.resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.login-page {
  min-height: 100vh;
  background: url(@/assets/login-bg.svg) no-repeat center / cover;
  display: flex;
  align-items: center;
  justify-content: space-around;
  .el-card {
    width: 420px;
    ::v-deep .el-card__header {
      height: 80px;
      background: rgba(114, 124, 245, 1);
      text-align: center;
      line-height: 40px;
      color: #fff;
      font-size: 18px;
    }
  }
  .el-form {
    padding: 0 20px;
  }
  .tc {
    text-align: center;
  }
}
</style>
